package com.cmict.data.system.api.sql;

import com.google.common.annotations.VisibleForTesting;

import java.util.regex.Pattern;
import static com.cmict.data.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cmict.data.system.enums.ErrorCodeConstants.PARAMS_VERIFY_CHARACTER_ONE;
import static com.cmict.data.system.enums.ErrorCodeConstants.PARAMS_VERIFY_CHARACTER_TWO;

public class SqlParameterCheck {

    @VisibleForTesting
    public static void checkMatch(String name) {
        if (name == null) {
            return;
        }
        if (!match(name)) {
            throw exception(PARAMS_VERIFY_CHARACTER_ONE);
        }
    }

    @VisibleForTesting
    public static void checkDigitalMatch(String name) {
        if (name == null) {
            return;
        }
        if (!digitalMatch(name)) {
            throw exception(PARAMS_VERIFY_CHARACTER_TWO);
        }
    }

    /**
     * 不能有特殊字符
     * @param str
     * @return
     */
    public static Boolean match(String str) {
        String regEx="^[a-zA-Z0-9_]*$";
        Boolean result = Pattern.compile(regEx).matcher(str).matches();
        return result;
    }

    /**
     * 只能包含数字
     * @param str
     * @return
     */
    public static Boolean digitalMatch(String str) {
        String regEx="^[0-9]*$";
        Boolean result = Pattern.compile(regEx).matcher(str).matches();
        return result;
    }

}
